package com.bingxu.flink.advancedcode;

import com.bingxu.flink.bean.WaterSensor;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.SlidingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.util.HashMap;

/**
 * @author :BingXu
 * @description :TODO
 * @date :2021/8/16 9:35
 * @modifier :
 */

public class SlidingWind {
    public static void main(String[] args) throws Exception {
        // env
        Configuration conf = new Configuration();
        conf.setInteger("port", 19999);//指定env运行的端口
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);
        streamSource
                .flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
                    @Override
                    public void flatMap(String value,
                                        Collector<Tuple2<String, Integer>> out) throws Exception {
                        String[] fields = value.split(" ");
                        for (String field : fields) {
                            out.collect(Tuple2.of(field, 1));
                        }

                    }
                })
                .keyBy(ele -> ele.f0)
                .window(SlidingProcessingTimeWindows.of(Time.seconds(5), Time.seconds(3)))
                .process(new ProcessWindowFunction<Tuple2<String, Integer>, String, String, TimeWindow>() {
                    HashMap<String, Integer> kvMap = new HashMap<>();
                    // 分组的key
                    String keyWord;

                    @Override
                    public void process(String s, Context context, Iterable<Tuple2<String, Integer>> elements, Collector<String> out) throws Exception {
                        for (Tuple2<String, Integer> element : elements) {
                            keyWord = element.f0;
                            Integer wNum = kvMap.get(element.f0);
                            if (wNum == null) {
                                kvMap.put(element.f0, 1);
                            }else{
                                kvMap.put(element.f0, wNum + 1);
                            }

                        }
                        out.collect(context.window().getStart() + " word=" + keyWord + " word count =" + kvMap.get(keyWord)
                                + "\t Endtime: " + context.window().getEnd());
                    }
                }).print();


        env.execute();

    }
}
